package Actions;

import java.awt.Dimension;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.Toolkit;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.FloatControl;
import javax.swing.JScrollBar;
import javax.swing.JScrollPane;

import Room.Room;

// for handling fullscreen and such
public class Windows {

	private boolean isFull = false;
	private TypeWriter.TypeWriter parent;
	
	private static final int DEFAULT_MARGIN = 150;
	private static final int DEFAULT_MARGIN_SMALL = 20;
	
	private Rectangle current;
	
	JScrollBar vscroller;
	
	// make it accessible to all
	public int scrollPos; // this will be the distance from the bottom of the scroller
	private boolean isUser;
	
	private Room editor;
	
	Clip sound;
	
	
	// construct new window handler
	public Windows( TypeWriter.TypeWriter parent, JScrollPane scroll, Room editor )
	{
		this.parent = parent;
		this.editor = editor;
		
		current = new Rectangle(0, 0, TypeWriter.TypeWriter.INIT_W, TypeWriter.TypeWriter.INIT_H);
		
		vscroller = this.parent.getScroller().getVerticalScrollBar();
		isUser = false;
		
		setUpSound();
	}
	
	public boolean isFull()
	{
		return isFull;
	}
	
	public void toggleFullScreen()
	{
		if(isFull)
		{
			parent.setVisible(false);
			parent.dispose();
			parent.setUndecorated(false);
			parent.setVisible(true);
			parent.setBounds(current);

			smallMargins();
			
			isFull = false;
		}
		else
		{
			parent.setVisible(false);
			parent.dispose();
			parent.setUndecorated(true);
			parent.setVisible(true);

			current.setBounds(parent.getBounds());
			
			Dimension size = Toolkit.getDefaultToolkit().getScreenSize();
			parent.setBounds(0, 0, (int)Math.ceil(size.getWidth()), (int)Math.ceil(size.getHeight()));
			
			bigMargins();
			
			isFull = true;
		}
	}
	
	public void smallMargins()
	{
		editor.setMargin( new Insets(DEFAULT_MARGIN_SMALL,DEFAULT_MARGIN_SMALL,DEFAULT_MARGIN_SMALL,DEFAULT_MARGIN_SMALL) );
	}
	public void bigMargins()
	{
		editor.setMargin( new Insets(DEFAULT_MARGIN_SMALL,DEFAULT_MARGIN, parent.getHeight()-editor.getLineHeight()*3 ,DEFAULT_MARGIN) );
	}
	
	public void setUser( boolean set )
	{
		isUser = set;
	}
	public boolean getUser( )
	{
		return isUser;
	}
	
	public Rectangle getBounds()
	{
		return current;
	}
	
	public void setUpSound()
	{

		try {
			sound = AudioSystem.getClip();
			sound.open(AudioSystem.getAudioInputStream( Windows.class.getResource("typewriter-key-1.wav") ));
			((FloatControl) sound.getControl(FloatControl.Type.MASTER_GAIN)).setValue(-20.0f);
		} 
		catch (Exception e){
			System.out.println("Unable to load sound");
		}
	}
	
	public void playSound()
	{
		if( sound!=null && parent.getOptions().soundOn)
		{
			sound.stop();
			sound.setFramePosition(0);
			sound.start();
		}
	}
	

	// just close thend dispose of thsi window
	public void closeDisposeWindow()
	{
		boolean option = parent.getFiles().saveOld();
		if( option )
		{
			parent.getOptions().save();
			parent.getOptions().disposeDialog();
			parent.getOptions().disposeHelp();
			parent.dispose();
			System.exit(0);
		}
	}
	
}
